Serial Ports


Serial Port abstraction for ATT.

</span>
This notebook shows the ATT Serial Port abstraction module.
This module was created for enabling testing on ATT framework. The Serial Port abstraction provides an Abstract base class so it can be extended and implement whatever kind of serial port we need. We have used this class hierarchy to build some Mocks, in order to test the ATT framework.

Set modules path first:


In [1]:
import sys
#sys.path.insert(0, '/home/asanso/workspace/att-spyder/att/src/python/')
sys.path.insert(0, 'i:/dev/workspaces/python/att-workspace/att/src/python/')

The main abstract base class is the following one:

class SerialPort: metaclass = abc.ABCMeta

@abc.abstractmethod
def isOpen(self):
    pass

@abc.abstractmethod
def readline(self):
    pass

@abc.abstractmethod
def close(self):
    pass

@abc.abstractmethod
def get_port(self):
    return ""

@abc.abstractmethod
def get_baudrate(self):
    return 0

As an example, we can see a dummy implementation:

class DummySerialPort (SerialPort): def init(self, port = None, baud = None): pass

def isOpen(self):
    return True

def close(self):
    pass

def get_port(self):
    return ""

def get_baudrate(self):
    return 0

def readline(self):
    time_delay = int(3*random.random())+1
    time.sleep(time_delay)
    return self.gen_random_line()

def gen_random_line(self):
    return "Hee"

Building Serial Ports

In order to build an instance of a SerialPort class, we have 2 options:

  • Call the constructor directly
  • Use a Builder
  • </ul> </span>

    Calling the constructor

    
    
    In [2]:
    import hit.serial.serial_port
    
    port=""
    baud=0
    dummySerialPort = hit.serial.serial_port.DummySerialPort(port, baud)
    

    The DummSerialPort is very simple. It just says "Hee" (after a few seconds) when its method "readline()" is called.
    Port and Baud are useless here.

    
    
    In [3]:
    print dummySerialPort.readline()
    
    
    
    
    Hee
    

    Let's create a more interesting Serialport instance,

    
    
    In [4]:
    import hit.serial.serial_port
    
    port=""
    baud=0
    emulatedSerialPort = hit.serial.serial_port.ATTEmulatedSerialPort(port, baud)
    

    The ATTEmulatedSerialPort will emulate a real ATT serial port reading.
    Port and Baud are useless here.

    
    
    In [5]:
    print emulatedSerialPort.readline()
    
    
    
    
    hit: {0 1048 3204 576 196 592 212 1 r}
    

    Using a Builder

    Let's use a builder now. We can choose the builder we want and build as many SerialPorts we want.

    
    
    In [6]:
    import hit.serial.serial_port_builder
    
    builder = hit.serial.serial_port_builder.ATTEmulatedSerialPortBuilder()
    
    port=""
    baud=0
    
    emulatedSerialPort1 = builder.build_serial_port(port, baud)
    emulatedSerialPort2 = builder.build_serial_port(port, baud)
    emulatedSerialPort3 = builder.build_serial_port(port, baud)
    emulatedSerialPort4 = builder.build_serial_port(port, baud)
    emulatedSerialPort5 = builder.build_serial_port(port, baud)
    emulatedSerialPort6 = builder.build_serial_port(port, baud)
    emulatedSerialPort7 = builder.build_serial_port(port, baud)
    

    And call "readline()"

    
    
    In [7]:
    print emulatedSerialPort5.readline()
    
    
    
    
    hit: {0 2080 8744 1076 884 1904 1184 1 r}
    

    There is a special Serial port abstraction that is fed from a file.
    This is useful when we want to "mock" the serial port and give it previously stored readings.
    This is interesting, for example, in order to reproduce, or visualize the repetition of an interesting set of hits in a game. Because Serial line is Real-Time, there are situations where it is needed to provide the ATT framework with a set of know hits, previously stored.

    We can use the data use in "Train points importer".

    
    
    In [8]:
    !head -10 train_points_import_data/arduino_raw_data.txt
    
    
    
    
    (6,6)
    hit: {0:25 1549:4 2757:4 1392:4 2264:7 1764:7 1942:5 2984:5 r}
    hit: {0:33 1521:6 2712:4 1364:4 2226:10 1894:10 1905:8 2932:5 r}
    hit: {0:34 1554:7 2766:5 1233:4 2273:10 1766:4 1951:12 2993:4 r}
    hit: {0:31 1667:6 2878:4 1345:4 2209:4 1880:8 2056:14 2935:4 r}
    hit: {0:28 1529:6 2737:5 1211:5 2244:9 1735:4 1920:6 2967:4 r}
    hit: {0:35 1525:8 2720:5 1207:6 2237:10 1744:5 1922:5 2939:5 r}
    hit: {0:9 1521:10 2746:5 1218:8 2251:9 1744:5 1929:4 2971:4 r}
    hit: {0:16 1694:6 2910:5 1372:4 2415:8 1913:6 2098:10 2965:4 r}
    hit: {0:5 1703:6 2911:5 1561:4 2416:9 1402:4 2094:6 3136:5 r}
    
    
    
    In [9]:
    import hit.serial.serial_port_builder
    
    builder = hit.serial.serial_port_builder.ATTHitsFromFilePortBuilder()
    
    port="train_points_import_data/arduino_raw_data.txt"
    baud=0
    
    fileSerialPort = builder.build_serial_port(port, baud)
    

    And now we will read some lines:

    
    
    In [12]:
    for i in range(20):
        print fileSerialPort.readline()
    
    
    
    
    hit: {975:5 1515:4 3011:5 827:7 2027:5 0:8 859:14 3408:5 r}
    hit: {955:5 1502:4 3067:8 807:9 2058:5 0:9 839:16 2780:4 r}
    hit: {818:4 1336:4 3040:6 666:5 1873:6 0:8 700:19 2747:4 r}
    hit: {958:6 1483:5 3019:7 824:10 2024:5 0:8 690:4 2733:4 r}
    hit: {988:5 1526:6 3217:6 838:6 2057:6 0:4 872:19 2093:4 r}
    hit: {954:5 965:5 2541:7 314:10 1522:5 0:8 171:12 2241:4 r}
    hit: {962:5 1473:5 3182:5 829:6 2023:6 0:9 861:18 2057:4 r}
    hit: {779:6 1301:4 3019:7 644:5 1860:6 0:6 678:19 2728:4 r}
    hit: {1493:4 1848:4 3411:8 1343:5 2215:7 0:13 521:14 1735:4 r}
    hit: {1325:4 1841:5 3233:6 1175:5 2048:6 0:34 344:16 1581:4 r}
    hit: {1490:5 1838:5 3357:8 1340:4 2204:6 0:25 514:15 1737:4 r}
    hit: {1463:6 1824:6 3202:5 1328:4 2022:4 0:32 510:12 1710:4 r}
    hit: {1491:9 1855:8 3252:7 1167:6 2052:6 0:37 525:11 2265:9 r}
    hit: {1455:4 1967:5 3325:6 1314:6 2174:7 0:7 505:20 2379:8 r}
    hit: {1466:5 1829:6 3218:5 1496:5 2030:4 0:30 513:12 1713:4 r}
    hit: {1488:6 1841:6 2544:4 1170:6 2045:6 0:39 527:13 2250:5 r}
    hit: {1319:4 1823:7 3209:5 1173:6 2040:5 0:35 357:20 2251:9 r}
    hit: {1318:5 1865:4 3256:9 1170:7 2060:7 0:41 357:14 1580:4 r}
    hit: {7103:4 1833:5 3212:7 1168:5 2037:7 0:36 342:21 2249:9 r}
    hit: {1969:7 2309:7 3731:8 1665:5 2520:6 0:5 854:23 2734:9 r}
    
    
    
    In [ ]: